//
//

#import "MBProgressHUD+KDZExtension.h"

@implementation MBProgressHUD (KDZExtension)


/**
 *  显示信息
 *
 *  @param message 信息内容
 *  @param delay   延迟时间
 */
+ (void)showMessageWithDelay:(NSString *)message delay:(NSTimeInterval)delay {
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:delay];
}

/**
 *  显示成功信息
 *
 *  @param message 信息内容
 */
+ (void)showSuccessWithMessage:(NSString *)message {
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_right"]];
    hud.mode = MBProgressHUDModeCustomView;
    hud.label.text = message;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2];
}

/**
 *  显示成功信息
 */
+ (void)showSucess {
    [self showSuccessWithMessage:@"交易成功"];
}

/**
 *  显示失败信息
 *
 *  @param message 信息内容
 */
+ (void)showErrorWithMessage:(NSString *)message {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_error"]];
    hud.mode = MBProgressHUDModeCustomView;
    hud.label.text= message;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2];
}

/**
 *  显示网络失败信息
 */
+ (void)showNetError {
    [self showErrorWithMessage:@"网络连接失败"];
}

/**
 *  显示服务器链接失败信息
 */
+ (void)showServerError {
    [self showErrorWithMessage:@"连接服务失败"];
}

/**
 *  显示错误
 */
+ (void)showError:(NSError *)error {
    if (error && error.userInfo) {
        NSString *message = [error.userInfo valueForKey:@"respMsg"];
        [self showErrorWithMessage:message];
    }
}

/**
 *  手动关闭MBProgressHUD
 */
+ (void)hideHUD
{
    [self hideHUDForView:nil];
}

/**
 *  手动关闭MBProgressHUD
 *
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(UIView *)view
{
    [self hideHUDForView:kKeyWindow animated:YES];
}

+ (void)showAlertView:(NSString *)respCode message:(NSString *)respMsg target:(id)target {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"错误" message:respMsg preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSSLog(@"点击知道了");
    }]];
    
    [target presentViewController:alertController animated:YES completion:nil];
}
@end
